# examples

# Let's get this party started!
from wsgiref.simple_server import make_server

import falcon


# Ctyun hello
class CtyunResource:
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200  # This is the default status
        resp.content_type = falcon.MEDIA_HTML  # Default is JSON, so override
        resp.text = (
            '''<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        h1 {
            font-size: 36px;
        }
        .content {
            position: relative;
            padding-left: 30px;
            color: #D4001E;
        }
        .content::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            width: 10px;
            height: 168px;
            background-color: #D4001E;
        }
    </style>
</head>
<body>
    <div class="content">
        <img src="" alt="天翼云">
        <h1>Welcome to Falcon!</h1>
        <p>本应用由天翼云函数计算Serverless应用创建</p>
    </div>
</body>
</html>'''
        )


# Falcon follows the REST architectural style, meaning (among
# other things) that you think in terms of resources and state
# transitions, which map to HTTP verbs.
class ThingsResource:
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200  # This is the default status
        resp.content_type = falcon.MEDIA_TEXT  # Default is JSON, so override
        resp.text = (
            '\nTwo things awe me most, the starry sky '
            'above me and the moral law within me.\n'
            '\n'
            '    ~ Immanuel Kant\n\n'
        )


# falcon.App instances are callable WSGI apps
# in larger applications the app is created in a separate file
app = falcon.App()

# Resources are represented by long-lived class instances
ctyun = CtyunResource()
things = ThingsResource()

# handle requests
app.add_route('/', ctyun)
app.add_route('/things', things)

if __name__ == '__main__':
    with make_server('', 8000, app) as httpd:
        print('Serving on port 8000...')

        # Serve until process is killed
        httpd.serve_forever()